package com.lwbldy.modules.system.controller;

import com.lwbldy.common.shiro.ShiroUtils;
import com.lwbldy.common.tools.R;
import com.lwbldy.common.tools.StringUtils;
import com.lwbldy.modules.system.domain.SysMenu;
import com.lwbldy.modules.system.domain.SysRole;
import com.lwbldy.modules.system.domain.SysUser;
import com.lwbldy.modules.system.service.SysMenuService;
import com.lwbldy.modules.system.vo.MenuVO;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/menu")
public class SysMenuController {

    @Autowired
    SysMenuService sysMenuService;

    /**
     * Describe: 基础路径
     * */
    private static String MODULE_PATH = "system/menu/";


    @GetMapping("treeMenu")
    @ResponseBody
    public List<MenuVO> treeMenu(){
        SysUser sysUser = ShiroUtils.getUserEntity();
        if(sysUser == null){
            return null;
        }
        return sysMenuService.queryMenuByUserId(sysUser.getUserId());
//        return sysMenuService.queryAllTreeMenu();
    }

    @GetMapping("main")
    @RequiresPermissions({"system:menu:main"})
    public String list(){
        return MODULE_PATH + "main";
    }

    @PostMapping("list")
    @ResponseBody
    @RequiresPermissions({"system:menu:main"})
    public R listJSON(){
        return R.ok(sysMenuService.listAllSysMenu());
    }

    @PostMapping("selectParent")
    @ResponseBody
    public R selectParent(){
        List<SysMenu> list = sysMenuService.listAllSysMenu();

        SysMenu rootMen = new SysMenu();
        rootMen.setMenuId(0L);
        rootMen.setName("顶级权限");
        rootMen.setParentId(-1L);
        list.add(rootMen);

        R r = R.ok(list);
        Map<String,Object> status = new HashMap<>();
        status.put("code",200);
        status.put("message","默认");
        r.put("status",status);
        return r;
    }

    /**
     * 获取菜单新增视图
     * @return
     */
    @GetMapping("add")
    @RequiresPermissions({"system:menu:save"})
    public String add(){
        return MODULE_PATH + "add";
    }

    /**
     * 保存
     * @param sysMenu
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    @RequiresPermissions({"system:menu:save"})
    public R save(@RequestBody SysMenu sysMenu){
        sysMenu.setStatus(1);
        return R.resultRow(sysMenuService.saveSysMenu(sysMenu));
    }

    /**
     * 获取菜单修改视图
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit")
    @RequiresPermissions({"system:menu:edit"})
    public String edit(Model model, long id){
        model.addAttribute("sysMenu",sysMenuService.queryById(id));
        return MODULE_PATH + "edit";
    }

    /**
     * 修改菜单信息
     * @param sysMenu
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    @RequiresPermissions({"system:menu:edit"})
    public R update(@RequestBody SysMenu sysMenu){
        return R.resultRow(sysMenuService.update(sysMenu));
    }


    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("remove/{id}")
    @ResponseBody
    @RequiresPermissions({"system:menu:remove"})
    public R remove(@PathVariable long id){
        int result = sysMenuService.remove(id);
        return R.resultRow(result);
    }

    @DeleteMapping("removeBatch/{ids}")
    @ApiOperation(value="批量删除角色数据")
    @ResponseBody
    @RequiresPermissions({"system:menu:remove"})
    public R removeBatch(@PathVariable String ids){
        int result = sysMenuService.removeBatch(StringUtils.StringIdsToArr(ids));
        return R.resultRow(result);
    }

    /**
     * Describe: 根据 Id 开启用户
     * Param powerId
     * Return ResuTree
     * */
    @PutMapping("enable")
    @ResponseBody
    @RequiresPermissions({"system:menu:edit"})
    public R enable(@RequestBody SysMenu sysMenu){
        sysMenu.setStatus(1);
        int result = sysMenuService.update(sysMenu);
        return R.resultRow(result);
    }

    /**
     * Describe: 根据 Id 禁用用户
     * Param powerId
     * Return ResuTree
     * */
    @PutMapping("disable")
    @ResponseBody
    @RequiresPermissions({"system:menu:edit"})
    public R disable(@RequestBody SysMenu sysMenu){
        sysMenu.setStatus(1);
        int result = sysMenuService.update(sysMenu);
        return R.resultRow(result);
    }

}
